<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        
        <a-button type="default"   @click="handleWlForm()">
          {{ t('物流信息批改') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          {{ t('物流信息导入') }}
        </a-button>
        
        <a-button type="default" @click="handlePrinfxForm()" v-auth="'wms:rd:order:rdOrder:edit'">
          <Icon icon="ant-design:printer-twotone" /> {{ t('封箱标签') }}
        </a-button>
       
       <a-button type="default" @click="handleExport()">
        <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
      </a-button>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'wms:rd:order:rdOrder:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno })">
          {{ record.djno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <PrinfxForm @register="registerPrinfxDrawer" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <BasicModal v-bind="$attrs" @register="registerGzModal" title="设置关账日期" @ok="handleQr">
        <BasicForm @register="registerForm" />
    </BasicModal>
    <InputWlForm @register="registerWlDrawer" @success="handleSuccess" />
    <Modal @register="registerModal" />
    
  </div>
</template>
<script lang="ts" setup name="ViewsWmsRdOrderRdOrderList">
  import { unref, onMounted,computed } from 'vue';
  import { Table } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Popconfirm } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rdOrderDelete, rdOrderListData,rdOrderConfirm,rdOrderBatchSend } from '/@/api/wms/rd/order/rdOrder';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { companyTreeData } from '/@/api/sys/company';
  import InputForm from './form.vue';
  import PrinfxForm from './prinfxForm.vue';
  import PrintModal from '/@/components/print/printModal.vue';
  import FormImport from './formImport.vue';
  import InputWlForm from './formWl.vue';
  import Modal from './Modal.vue';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('wms.rd.order.rdOrder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('出库订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'companyCode',
        fieldLabel: 'company.companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('出库单号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('发货单号'),
        field: 'fbillno',
        component: 'Input',
      },
      {
        label: t('拣货单号'),
        field: 'pickCode',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'basWare.cwhname',
        component: 'Input',
      },
      {
        label: t('推送日期'),
        field: 'sendDate',
        component: 'RangePicker',
      },
      {
        label: t('发货日期'),
        field: 'fdate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
          valueFormat: 'YYYY-MM-DD',
        },
      },
      
      {
        label: t('联系人'),
        field: 'fperson',
        component: 'Input',
      },
      
      {
        label: t('电话'),
        field: 'fphone',
        component: 'Input',
      },
      {
        label: t('地址'),
        field: 'faddress',
        component: 'Input',
      },
      {
        label: t('物流单号'),
        field: 'wlno',
        component: 'Input',
      },
      {
        label: t('物流公司'),
        field: 'wlcompany',
        component: 'Input',
      },
      {
        label: t('物流件数'),
        field: 'num',
        component: 'Input',
      },
      {
        label: t('物流日期'),
        field: 'wldate',
        component: 'Input',
      },
      {
        label: t('摘要'),
        field: 'fexplanation',
        component: 'Input',
      },
      {
        label: t('紧急程度'),
        field: 'furgentlevel',
        component: 'Select',
        componentProps: {
          dictType: 'wms_jj_level',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'rd02_ck_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('是否推送'),
        field: 'bsend',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [['sendDate', ['sendDate_gte', 'sendDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
  {
      title: t('公司名称'),
      dataIndex: 'company.companyName',
      key: 'company.company_name',
      sorter: true,
      width: 160,
      align: 'left',
    },
    {
      title: t('出库单号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('发货单号'),
      dataIndex: 'fbillno',
      key: 'a.fbillno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('拣货单号'),
      dataIndex: 'pickCode',
      key: 'a.pick_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('紧急程度'),
      dataIndex: 'furgentlevel',
      key: 'a.furgentlevel',
      dictType: 'wms_jj_level',
      sorter: true,
      width: 100,
      align: 'left',
    },
    
    {
      title: t('推送日期'),
      dataIndex: 'sendDate',
      key: 'a.send_date',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('发货日期'),
      dataIndex: 'fdate',
      key: 'a.fdate',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库'),
      dataIndex: 'basWare.cwhname',
      width: 100,
      align: 'left',
    },
    {
      title: t('联系人'),
      dataIndex: 'fperson',
      key: 'a.fperson',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('电话'),
      dataIndex: 'fphone',
      key: 'a.fphone',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('地址'),
      dataIndex: 'faddress',
      key: 'a.faddress',
      sorter: true,
      width: 230,
      align: 'left',
    },
    // {
    //   title: t('备货状态'),
    //   dataIndex: 'bhStatus',
    //   key: 'a.bhStatus',
    //   dictType: 'jh_bh_status',
    //   sorter: true,
    //   width: 100,
    //   align: 'center',
    // },
    {
      title: t('数量'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 80,
      align: 'right',
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('重量'),
      dataIndex: 'fweight',
      key: 'a.fweight',
      sorter: true,
      width: 80,
      align: 'right',
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('收货单位'),
      dataIndex: 'fsuppliername',
      key: 'a.fsuppliername',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('物流单号'),
      dataIndex: 'wlno',
      key: 'a.wlno',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('物流公司'),
      dataIndex: 'wlcompany',
      key: 'a.wlcompany',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('物流件数'),
      dataIndex: 'num',
      key: 'a.num',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('物流日期'),
      dataIndex: 'wldate',
      key: 'a.wldate',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('是否打包'),
      dataIndex: 'rdrecord32.fifpack',
      key: 'rd32.fifpack',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('摘要'),
      dataIndex: 'fexplanation',
      key: 'a.fexplanation',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('推送'),
      dataIndex: 'bsend',
      key: 'a.bsend',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      fixed: 'right',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'rd02_ck_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑出库订单'),
        onClick: handleForm.bind(this, { djno: record.djno }),
        auth: 'wms:rd:order:rdOrder:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除出库订单'),
        popConfirm: {
          title: t('是否确认删除出库订单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wms:rd:order:rdOrder:edit',
      },
    ],
  };

  const [registerWlDrawer, { openDrawer: wlOpenDrawer }] = useDrawer();
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerModal, { openModal }] = useModal();
  
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPrinfxDrawer, { openDrawer: openPrinfxDrawer}] = useDrawer();
  
  const [registerTable, { reload, getForm, getSelectRowKeys, getSelectRows,getDataSource, setProps }] = useTable({
    api: rdOrderListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
    showCustomSummary: true,
    showTotalCustomSummary: true,
  });

  const [registerImportModal, { openModal: importModal }] = useModal();
  const [registerGzModal, { openModal: importGzModal, closeModal: closeGzModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  

  async function handlets() {
    if (getSelectRows().length) {
      let selIds =  getSelectRows().map((item) => item.djno).join(',')
      const res = await rdOrderBatchSend({ selIds });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择数据');
    }
  }

  async function handleConfirm() {
    if (getSelectRows().length) {
      let selIds =  getSelectRows().map((item) => item.djno).join(',')
      const res = await rdOrderConfirm({ selIds });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择数据');
    }
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handlePrinfxForm() {
    let arr = await getSelectRowKeys();
    if (getSelectRowKeys().length != 1) {
      showMessage(t('请选择一行数据'));
      return;
    }
    openPrinfxDrawer(true, { djno: arr[0]});
  }

  async function handleWlForm(record: Recordable) {
    let arr = await getSelectRowKeys();
    if (getSelectRowKeys().length) {
      let selIds = arr.join(',')
      wlOpenDrawer(true, {selIds});
    }else{
      showMessage('请先选择数据');
    }
  }

   // 打印
   async function handleTpPrint() {
    let arr = await getSelectRowKeys();
    if (getSelectRowKeys().length == 0) {
      showMessage(t('请先选择数据'));
      return;
    }
    let params = {
      ids: arr.join(','),
      title: '托盘标签打印',
      height: '0.9',
      width: '0.9',
      fileName: '托盘',
    };
    openPrintModal(true, params);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wms/rd/order/rdOrder/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  async function handleShdPrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择数据'));
      return;
    }
    let newArr = arr.map(item=>{
      return item.djno
    })
    let params = {
      ids: newArr.join(','),
      title: '出库单打印',
      height: '0.9',
      width: '0.9',
      fileName: '出库单',
    };
    openPrintModal(true, params);
  }

  
  async function handleDelete(record: Recordable) {
    const params = { djno: record.djno };
    const res = await rdOrderDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(async () => {
    setProps()
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      bsend:'0'
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      bsend:'0'
    });
  }

  const totalFqty = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    return sum;
  });

  const totalFweight = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fweight || 0), 0);
    sum = sum.toFixed(2)
    return sum;
  });

  async function handleGzForm(record: Recordable) {
    // if (getSelectRows().length) {
    //   let selIds = getSelectRows()[0].id
    //   importGzModal(true);
    // }else{
    //   showMessage('请先选择数据');
    // }
    openModal(true,{});
  }
</script>
